package heap;

import java.util.HashSet;
import java.util.PriorityQueue;

public class findKthLargest {
   public int findKthLargest(int[] nums, int k) {
        //先去重
        // HashSet<Integer> set = new HashSet<>();
        // for(int item:nums){
        //     set.add(item);
        // }
        //存入heap
        PriorityQueue<Integer> heap = new PriorityQueue<>((a,b)->b-a);
        for(Integer item: nums){
            heap.offer(item);
        }
        for(int i=0;i<k;i++){
            if (i==k-1) {
                return (int) heap.poll();
            }
            heap.poll();
        }
        return 0;
    } 
    public static void main(String[] args) {
        // int[] arr = new int[]{3,2,1,5,6,4};
        int[] arr = new int[]{3,2,3,1,2,4,5,5,6};
        findKthLargest obj = new findKthLargest();
        System.out.println(obj.findKthLargest(arr, 4));
    }
}
